package com.mars.tukuan.web.controller;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.base.BaseController;
import com.mars.base.common.base.BaseResult;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.Paginator;
import com.mars.component.mq.JmsQueueMessage;
import com.mars.tukuan.dto.CarMessage;
import com.mars.tukuan.dto.CarStateVo;
import com.mars.tukuan.entity.CarState;
import com.mars.tukuan.rpc.api.CarService;
import com.mars.tukuan.rpc.api.CarStateService;
import com.mars.tukuan.rpc.api.CarUserService;
import com.mars.tukuan.utils.file.FileMeta;
import com.mars.tukuan.utils.file.FileUtil;
import com.mars.tukuan.utils.file.cloud.UploadCloudStroe;
import com.mars.tukuan.utils.file.cloud.UploadResult;

@RestController
@RequestMapping("/api/tukuan/mobile/car/state")
public class CarStateController extends BaseController {

	@Autowired
	private CarStateService service;
	
	@Autowired
	private CarService carService;
	
	@Autowired
	private UploadCloudStroe uploadCloudStroe;
	
	@Autowired
	private CarUserService carUserService;
	
	@Autowired
	private JmsQueueMessage jmsQueueMessage ;

	@PostMapping(value = "/add")
	public Object add(CarStateVo entity) {
		try {
			this.service.insertSelective(entity);
		} catch (Exception e) {
			return BaseResult.addFailed();
		}
		return BaseResult.addOk();
	}

	@GetMapping(value = "/delete/{ids}")
	public Object delete(@PathVariable String ids) {
		try {
			this.service.deleteByIds(ids);
		} catch (Exception e) {
			return BaseResult.deleteFailed();
		}
		return BaseResult.deleteOk();
	}

	@PostMapping(value = "/update")
	public Object update(CarStateVo entity) {
		try {
			entity.setAfterTime(new Date());
			CarState selectById = this.service.selectById(entity.getId()) ;

			if(entity.getAfterImageUrl() == null) {
				selectById.setAfterDesc(null);
				selectById.setAfterImageUrl(null);
				selectById.setAfterUserId(null);
			} else {
				selectById.setAfterDesc(entity.getAfterDesc());
				selectById.setAfterImageUrl(entity.getAfterImageUrl());
				//上传人的ID，要等加入登录后才能完成
				//selectById.setAfterUserId(entity.getAfterUserId());
			}
			
			if(entity.getBeforeImageUrl() == null) {
				selectById.setBeforeDesc(null);
				selectById.setBeforeImageUrl(null);
				//上传人的ID，要等加入登录后才能完成
				//selectById.setBeforeUserId(null);
			} else {
				selectById.setBeforeDesc(entity.getBeforeDesc());
				selectById.setBeforeImageUrl(entity.getBeforeImageUrl());
				//上传人的ID，要等加入登录后才能完成
				//selectById.setBeforeUserId(entity.getBeforeUserId());
			}
			
			this.service.update(selectById) ;
			//this.service.updateSelective(entity);
		} catch (Exception e) {
			return BaseResult.updateFailed();
		}
		return BaseResult.updateOk();
	}

	@GetMapping(value = "/get/{id}")
	public CarState get(@PathVariable Long id) {
		return this.service.selectById(id);
	}
	
	@GetMapping(value = "/list")
	public Object list(CarStateVo vo) {
		try {
			return  this.service.list(new Paginator(request), vo) ;
		} catch (AppException e) {
			return BaseResult.loadFailed();
		}
		
	}

	@GetMapping(value = "/selectPageCarState")
	public Object selectPageCarState(CarStateVo vo) {
		try {
			return  this.service.selectPageCarState(new Paginator(request), vo) ;
		} catch (AppException e) {
			return BaseResult.loadFailed();
		}
		
	}
	
	/**
	 * 处理前（新伤）
	 * @param entity
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/beboreUpload")
	public Object beboreUpload(CarState entity, HttpServletRequest request) {
		try {
			CarMessage message = new CarMessage() ;
			message.setCarState(entity);
			message.setFileMeta(FileUtil.upload(request));
			
			this.jmsQueueMessage.send("car.beboreUpload", message);
		} catch (Exception e) {
			return BaseResult.failure();
		}
		
		return BaseResult.addOk();
	}
	
	/**
	 * 处理后
	 * @param entity
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/afterUpload")
	public Object afterUpload(CarState entity, HttpServletRequest request) {
		try {
			CarMessage message = new CarMessage() ;
			message.setCarState(entity);
			message.setFileMeta(FileUtil.upload(request));
			
			this.jmsQueueMessage.send("car.afterUpload", message);
		} catch (Exception e) {
			return BaseResult.failure();
		}
		
		return BaseResult.addOk();
	}
	
	/**
	 * 后台修改处理前和处理后的图片上传
	 * @param entity
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/upload")
	public Object upload(HttpServletRequest request) {
		try {
			List<FileMeta> upload = FileUtil.upload(request) ;
			if(!upload.isEmpty()) {
				FileMeta fileMeta = upload.get(0);
				String filePath = fileMeta.getDestFilePath() ;
				
				UploadResult result = this.uploadCloudStroe.uploadFile(new File(filePath)) ;
				if(result.getStatus()) {
					//删除临时目录下的文件
					FileUtil.deleteFile(filePath);
					
					return BaseResult.build(true, "上传成功", result) ;
				}
			}
			
		} catch (Exception e) {
			return BaseResult.failure();
		}
		return BaseResult.ok();
	}
	
	/**
	 * 无伤
	 * @param entity
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/noHarm")
	public Object noHarm(CarStateVo entity) {
		try {
			this.service.insertSelective(entity) ;
			
			//更新车况检查时间
			this.carService.updateCheckTime(entity.getCarId());
			//更新用户的车况见时间
			this.carUserService.updateCheckTime(entity.getBeforeUserId());
		} catch (Exception e) {
			return BaseResult.failure();
		}
		
		return BaseResult.addOk();
	}
	
	@PostMapping(value = "/compensate")
	public Object compensate(CarStateVo entity) {
		try {
			System.out.println(entity);
			this.service.compensate(entity) ;
		} catch (Exception e) {
			return BaseResult.failure();
		}
		
		return BaseResult.addOk();
	}
	
	@PostMapping(value = "/follow")
	public Object follow(CarStateVo entity) {
		try {
			this.service.follow(entity) ;
		} catch (Exception e) {
			return BaseResult.failure();
		}
		
		return BaseResult.addOk();
	}
	
	/**
	 * 测试文件上传
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/test_upload")
	public Object test_upload(HttpServletRequest request) {
		try {
			List<FileMeta> upload = FileUtil.upload(request) ;
			if(!upload.isEmpty()) {
				FileMeta fileMeta = upload.get(0);
				System.out.println(fileMeta.getDestFilePath());
			}
		} catch (Exception e) {
			return BaseResult.failure();
		}
		return BaseResult.ok();
	}
	
}
